package com.aistar.service.user.impl;

import com.aistar.mapper.EmployeeMapper;
import com.aistar.mapper.ResumeMapper;
import com.aistar.mapper.ResumeViewMapper;
import com.aistar.mapper.UserMapper;
import com.aistar.model.dto.user.ResumeDTO;
import com.aistar.model.entity.Employee;
import com.aistar.model.entity.Resume;
import com.aistar.model.entity.ResumeView;
import com.aistar.model.entity.User;
import com.aistar.service.user.IEmployeeInfoService;
import com.aistar.service.user.IResumeCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmployeeInfoSeriveImpl implements IEmployeeInfoService {
    @Autowired
    private ResumeViewMapper resumeViewMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private IResumeCenterService resumeCenterService;
    @Override
    public List<ResumeView> loadAll() {
//        =resumeViewMapper.loadAll();
        return resumeViewMapper.loadAll();
    }


    @Override
    public Employee info(Integer employeeId) {
        return employeeMapper.findById(employeeId);
    }
    @Override
    public User UserInfo(Integer employeeId) {
        User user=userMapper.findById(employeeMapper.findById(employeeId).getUserId());
        return user;
    }

    @Override
    public ResumeDTO newResume(Integer employeeId) {
        List<ResumeDTO> resume = resumeCenterService.myResume(employeeId);
        ResumeDTO resumeNew=resume.get(0);
        for(int i = 1;i<resume.size();i++){
            if (resume.get(i).getResume().getUpdateTime().getTime()>resume.get(i-1).getResume().getUpdateTime().getTime()){
                resumeNew=resume.get(i);
            }
        }
        return resumeNew;
    }

    @Override
    public Integer updateTime(Integer employeeId) {
        Resume resume = newResume(employeeId).getResume();
        resume.setUpdateTime(new Date());
        return resumeMapper.updateUpdateTimeById(resume);
    }

    @Override
    public Integer privacyChange(Integer resumeId) {
        Resume resume =resumeMapper.findById(resumeId);
        if (0==resume.getPrivacy()){
            resume.setPrivacy(1);
            return resumeMapper.updatePrivacyById(resume);
        }
        resume.setPrivacy(0);
        return resumeMapper.updatePrivacyById(resume);
    }
}
